#include "player.h"
#include "main_data.h"
#include "tool.h"

Player::Player()
{
	is_alive = true;
	destroy_img_index = 0;
	speed = 8;
	life = 5;
	score = 0;
	full_screen_bomb = 5;
	invincible = false;
	loadimage(&img[0], "resource/images/me1.png");
	loadimage(&img[1], "resource/images/me2.png");

	loadimage(&img_destroy[0], "resource/images/me_destroy_1.png");
	loadimage(&img_destroy[1], "resource/images/me_destroy_2.png");
	loadimage(&img_destroy[2], "resource/images/me_destroy_3.png");
	loadimage(&img_destroy[3], "resource/images/me_destroy_4.png");

}

void Player::init()
{
	is_alive = true;
	destroy_img_index = 0;
	speed = 8;
	life = 5;
	score = 0;
	full_screen_bomb = 5;
	invincible = false;
	x = WINDOW_WIDTH / 2 - HERO_PLANE_WIDTH / 2;
	y = WINDOW_HEIGHT - HERO_PLANE_HEIGHT;
}

void Player::move(short direction)
{
	switch (direction)
	{
	case 0:
		if (x > 0)
		{
			x -= speed;
		}
		break;
	case 1:
		if (x < WINDOW_WIDTH - HERO_PLANE_WIDTH)
		{
			x += speed;
		}
		break;
	case 2:
		if (y > 0)
		{
			y -= speed;
		}
		break;
	case 3:
		if (y < WINDOW_HEIGHT - HERO_PLANE_HEIGHT)
		{
			y += speed;
		}
		break;
	}

}

void Player::draw()
{
	if (is_alive)
	{
		if (tmer_img.isTimeOut(100))
		{
			drawAlpha(x, y, &img[0]);
		}
		else
		{
			drawAlpha(x, y, &img[1]);
		}
		if (invincible)
		{
			if (tmer_hero_invincible.isTimeOut(3000))
			{
				invincible = 0;
			}
		}

	}
	else
	{
		if (tmer_img_destroy.isTimeOut(200)) {
			drawAlpha(x, y, &img_destroy[destroy_img_index++]);
			if (destroy_img_index == 3)
			{
				resetPosition();
				invincible = 1;
				tmer_hero_invincible.setTime();
			}

		}
	}


}


void Player::resetPosition()
{

	is_alive = true;
	destroy_img_index = 0;
	x = WINDOW_WIDTH / 2 - HERO_PLANE_WIDTH / 2;
	y = WINDOW_HEIGHT - HERO_PLANE_HEIGHT;
}
